package yf.mall.controller;

import java.util.List;

import com.yf.request.SearchParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import java.util.Map;

import com.yf.consts.DaoConst;
import yf.mall.entity.AreaEntity;
import yf.mall.service.AreaService;
import com.yf.response.R;
import yf.mall.vo.AreaVo;


/**
 * 区域代理Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu May 07 14:16:15 CST 2020
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 列表区域代理
     */
    @RequestMapping("/list")
    @RequiresPermissions("area:list")
    public R list(@RequestBody SearchParam<AreaVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit());
        List<AreaVo> areaList = areaService.queryListVo(params.getEntity());
        return R.ok(new PageInfo(areaList));
    }

    /**
     * 查看区域代理
     */
    @SysLog("查看区域代理")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("area:info")
    public R info(@PathVariable("id") Long id) {
        AreaEntity area = areaService.get(id);

        return R.ok(area);
    }

    /**
     * 保存区域代理
     */
    @SysLog("保存区域代理")
    @RequestMapping("/save")
    @RequiresPermissions("area:save")
    public R save(@RequestBody AreaVo areaVo) {

        areaService.merge(areaVo);
        return R.ok(areaVo);
    }

    /**
     * 修改区域代理
     */
    @SysLog("修改区域代理")
    @RequestMapping("/update")
    @RequiresPermissions("area:update")
    public R update(@RequestBody AreaVo areaVo) {
        System.out.printf(areaVo.getArea().getName());
        areaService.merge(areaVo);
        return R.ok(areaVo);
    }

    /**
     * 删除区域代理
     */
    @SysLog("删除区域代理")
    @RequestMapping("/delete")
    @RequiresPermissions("area:delete")
    public R delete(Long[] ids) {
        areaService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有区域代理
     */
    @SysLog("所有区域代理")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AreaEntity> areaList = areaService.queryList(params);

        return R.ok(areaList);
    }

    /**
     @ModelAttribute public SearchParam<AreaEntity> searchParam(SearchParam<AreaEntity> params, AreaEntity area) {
     if (area != null) {
     params.setEntity(area);
     }

     return params;
     }
     **/
    /***********自定义开始***********/
}
